#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/4/7
from Kuaidaili.kuaidaili import KuaiDaili
from Kuaidaili.FreeDaili import Dali
from bs4 import BeautifulSoup
import json


class Parser(object):
    def getJson(self, page=1):
        daili = KuaiDaili()

        html = daili.getSoup(page)
        list_tr = html.find_all("tr")

        list_daili = []
        for tr in list_tr:
            soup_tr = BeautifulSoup(str(tr), "lxml")
            if soup_tr.find("td") is not None:
                list_td = soup_tr.find_all("td")
                ip = list_td[0].text
                port = list_td[1].text
                type_ = list_td[2].text
                anonymous = list_td[3].text

                location = list_td[4].text
                speed = list_td[5].text
                verify_time = list_td[6].text
                d = Dali(ip, port, type_, anonymous, location, speed, verify_time)
                list_daili.append(d.IP)
        return json.dumps(list_daili, ensure_ascii=False)


p = Parser()
print(p.getJson(1))
